import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';
import qiankun from 'vite-plugin-qiankun';
const pkg = await import('./package.json', { assert: { type: 'json' } });
const packageName = pkg.name;

// https://vite.dev/config/
export default defineConfig({
  plugins: [react(), qiankun(packageName, { useDevMode: true })],
  server: {
    host: 'localhost', // 指定服务器应该监听哪个IP地址,默认：localhost
    port: 3003, // 指定开发服务器端口,默认：5173
    cors: true, // 允许跨域
    proxy: {
      // 为开发服务器配置自定义代理规则
      // 带选项写法：http://localhost:5173/api/posts -> http://jsonplaceholder.typicode.com/posts
      '/api/bizhi': {
        target: 'https://tea.qingnian8.com', // 目标接口
        changeOrigin: true, // 是否换源
        // rewrite: (path) => path.replace(/^\/api/, ""),
      },
    },
    headers: {
      'Access-Control-Allow-Origin': '*', // TODO: ❗️ 关键配置项
    },
  },
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
  },
  // TODO: ❗️ 关键配置项
  build: {
    target: 'esnext',
    outDir: 'dist',
    lib: {
      entry: path.resolve(__dirname, 'src/main.tsx'),
      name: `${packageName}-[name]`,
      fileName: format => `${packageName}.${format}.js`,
      formats: ['umd'],
    },
    rollupOptions: {
      output: {
        chunkFileNames: `[name]-${packageName}-[hash].js`,
        globals: {
          react: 'React',
          'react-dom': 'ReactDOM',
        },
      },
    },
  },
});
